/** 
  成绩录入开关--新增权限
*/
<template>
  <div class="add-right-container">
    <div style="height: 40px;">
      <el-breadcrumb separator="/" class="float-left" style="padding-top: 8px;">
        <el-breadcrumb-item :to="{ path: '/scoreEntrySwitch' }">
          <span @click="backToMain()">{{ $t('scoreEntrySwitch.scoreEntrySwitch') }}</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>{{ $t('scoreEntrySwitch.addRight') }}</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="float-right">
        <el-button type="text" @click="backToMain">{{ $t('common.btn.back') }}</el-button>
      </div>
    </div>
    <el-card>
      <h1 class="condition-title">{{ $t('scoreEntrySwitch.addRight') }}</h1>
      <el-form
        ref="addForm"
        :model="addForm"
        :rules="addRules"
        label-position="right"
        label-width="150px">
          <el-row :gutter="24">
            <!-- 学期 -->
            <el-col :span="8">
              <el-form-item :label="$t('scoreEntrySwitch.semester') + '：'" prop="calendarId">
                <calendar-select v-model="addForm.calendarId" clearable :placeholder="$t('common.pla.pleaseSelect')"></calendar-select>
              </el-form-item>
            </el-col>
            <!-- 开始时间 -->
            <el-col :span="8">
              <el-form-item :label="$t('scoreEntrySwitch.startTime') + '：'" prop="startTime">
                <el-date-picker
                  v-model="addForm.startTime"
                  :placeholder="$t('common.pla.pleaseSelect')"
                  :picker-options="addPickerBeginDateBefore"
                  clearable
                  type="datetime"
                  format="yyyy/MM/dd HH:mm:ss"
                  align="right"
                  value-format="timestamp"
                  style="width: 100%">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <!-- 结束时间 -->
            <el-col :span="8">
              <el-form-item :label="$t('scoreEntrySwitch.endTime') + '：'" prop="stopTime">
                <el-date-picker
                  v-model="addForm.stopTime"
                  :placeholder="$t('common.pla.pleaseSelect')"
                  :picker-options="addPickerBeginDateAfter"
                  clearable
                  type="datetime"
                  format="yyyy/MM/dd HH:mm:ss"
                  align="right"
                  value-format="timestamp"
                  style="width: 100%">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24" class="lineStyle">
            <!-- 可录入类型 -->
            <el-col :span="8">
              <el-form-item :label="$t('scoreEntrySwitch.enteredTypes') + '：'" prop="enterType">
                <el-select v-model="addForm.enterType" :placeholder="$t('common.pla.pleaseSelect')" @change="changeEnterType" clearable style="width: 100%">
                  <el-option
                    v-for="item in options.enterTypeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 关联展示可录入类型成绩 -->
            <!-- <el-col :span="16">
              <el-form-item label="" prop="relatedEnteredTypes">
                <el-input v-model="addForm.relatedEnteredTypes" readonly clearable :placeholder="$t('scoreEntrySwitch.relatedEnteredTypes')"></el-input>
              </el-form-item>
            </el-col> -->
          </el-row>
        <el-row :gutter="24" class="lineStyle">
          <!-- 总评成绩记录方式 -->
          <el-col :span="8" v-if="addForm.enterType === 2">
            <el-form-item :label="$t('scoreEntrySwitch.totalStandardRecord') + '：'" prop="totalStandardRecord">
              <el-select v-model="addForm.totalStandardRecord" style="width: 100%" >
                <el-option
                  v-for="item in options.recordTypeList"
                  :key="item.key"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 重考成绩记录方式 -->
          <el-col :span="8" v-if="addForm.enterType === 1">
            <el-form-item :label="$t('scoreEntrySwitch.reExamRecord') + '：'" prop="reExamRecord">
              <el-select v-model="addForm.reExamRecord" style="width: 100%">
                <el-option
                  v-for="item in options.recordTypeList"
                  :key="item.key"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="college-margin">
          <!-- 开课学院 -->
          <el-col :span="16">
            <el-form-item :label="$t('scoreEntrySwitch.startCollege') + '：'" prop="facultyIds" ref="facultyIds">
              <el-transfer
                v-model="addForm.facultyIds"
                :filter-placeholder="$t('scoreEntrySwitch.collegeNameSearch')"
                :titles="[$t('scoreEntrySwitch.allColleges'), $t('scoreEntrySwitch.choosedColleges')]"
                :data="options.facultyIds"
                @change="(value) => changeFacultyIds(value, 'facultyIds')"
                filterable>
              </el-transfer>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row :gutter="24">
        <el-col :span="16" style="text-align:right; margin-top: 20px;">
          <el-button @click="modelCancel()">{{ $t('score.cancle') }}</el-button>
          <el-button type="primary" @click="modelSure('addForm')">{{ $t('common.btn.confirm') }}</el-button>
        </el-col>
      </el-row>
  </el-card></div>
</template>
<script>
import { queryDic } from 'common/src/api/dictionary';
import UTILS from 'common/src/utils/utils';
import scoreManagementApi from 'common/src/api/scoreManagement/scoreManagement';
export default {
    props: {
      
    },
    data() {
      return {
        addForm: {
          calendarId: '', // 学期
          startTime: '', // 开始时间
          stopTime: '', // 结束时间
          enterType: 2, // 可录入类型，默认展示“期终成绩”
          relatedEnteredTypes: '', // 关联展示可录入类型成绩
          totalStandardRecord: '1002', // 总评成绩记录方式，默认五级制
          reExamRecord: '1003', // 重考成绩记录方式，默认两级制
          facultyIds: [], // 开课学院
        },
        userInfo: {},
        finalScoreList: '',
        finalScoreCodeList: '',
        reSuspendedResults: '重考成绩,缓考成绩',
        options: {
          enterTypeList: [
            {
              label: '期终成绩',
              key: 2,
              value: 2
            },
            {
              label: '重缓考成绩',
              key: 1,
              value: 1
            }
          ],
          // 开课学院
          facultyIds: [],
          // 可录入类型
          recordTypeList: []
        },
        addPickerBeginDateBefore: {
          disabledDate: time => {
            let beginDateVal = this.addForm.stopTime;
            if (beginDateVal) {
              return time.getTime() > beginDateVal;
            }
          }
        },
        addPickerBeginDateAfter: {
          disabledDate: time => {
            let beginDateVal = this.addForm.startTime;
            if (beginDateVal) {
              return time.getTime() < beginDateVal;
            }
          }
        },
        facultyCnsObj: {},
        addRules: {
          // 学期
          calendarId: [
            {
              required: true,
              message: this.$t('scoreEntrySwitch.requireSemester'),
              trigger: 'blur'
            }
          ],
          // 开始时间
          startTime: [
            {
              required: true,
              message: this.$t('scoreEntrySwitch.requireStartTime'),
              trigger: 'blur'
            }
          ],
          // 结束时间
          stopTime: [
            {
              required: true,
              message: this.$t('scoreEntrySwitch.requireEndTime'),
              trigger: 'blur'
            }
          ],
          // 可录入类型
          enterType: [
            {
              required: true,
              message: this.$t('scoreEntrySwitch.requireEnteredTypes'),
              trigger: 'change'
            }
          ],
          // 开课学院
          facultyIds: [
            {
              required: true,
              message: this.$t('scoreEntrySwitch.requireStartCollege'),
              trigger: 'blur'
            }
          ]
        }
      }
    },
    created() {
      // 获取当前用户信息
      if (localStorage.getItem("sessiondata")) {
        if (JSON.parse(localStorage.getItem("sessiondata")).mockUser) {
          this.userInfo = JSON.parse(localStorage.getItem("sessiondata")).mockUser
        } else {
          this.userInfo = JSON.parse(localStorage.getItem("sessiondata")).user
        }
      }
    },
    async mounted() {
      this.initOptions()
      // await this.selectGlobalSetInfo()
      // this.changeEnterType(2)
    },
    methods: {
      // 修改可录入类型
      changeEnterType(val) {
        if (!val) {
          this.addForm.relatedEnteredTypes = ''
        } else if (val === 2) {
          this.addForm.relatedEnteredTypes = this.finalScoreList
        } else {
          this.addForm.relatedEnteredTypes = this.reSuspendedResults
        }
      },
      // 获取期终成绩关联的可录入类型成绩
      async selectGlobalSetInfo() {
        let globalInfoRes = await scoreManagementApi.queryGlobalSetInfo({
          "setType": "totalGrade"
        })
        if (globalInfoRes.code === 200) {
          if (globalInfoRes.data.length > 0) {
            this.finalScoreList = globalInfoRes.data[0].setContentCn
            this.finalScoreCodeList = globalInfoRes.data[0].setContentCode
          }
        }
      },
      // 返回主页
      backToMain() {
        this.$emit('score-manage-reset-index-key')
      },
      initOptions() {
        // 筛选条件字典查询
        let data = {
          lang: this.$store.getters.language || "cn",
          type: "allChild",
          keys: [
            "X_YX", // 学院
            "X_CJJL_FX" // 成绩记录方式
          ]
        }
        queryDic(data).then(res => {
          UTILS.fillSelect(
            {
              facultyIds: "X_YX", // 学院
              recordTypeList: "X_CJJL_FX"
            },
            res.data,
            this.options
          )
        })
      },
      // 校验表单
      modelSure(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            this.addConfirmSure()
          } else {
            return false
          }
        });
      },
      // 修改开课学院
      changeFacultyIds(val, ref) {
        if (val.length > 0) {
          this.$refs[ref].clearValidate()
        }
      },
      addConfirmSure() {
        let facultyCns = ''
        if (this.addForm.facultyIds.length > 0) {
          facultyCns = UTILS.translateMultiSelectToStr(this.options.facultyIds, this.addForm.facultyIds)
        } else {
          facultyCns = ''
        }
        let params = {
          'calendarId': this.addForm.calendarId.toString(), // 校历ID（学年学期）
          'startTime': new Date(this.addForm.startTime).getTime(), // 开始时间
          'stopTime': new Date(this.addForm.stopTime).getTime(), // 结束时间
          'facultyIds': this.addForm.facultyIds.length > 0 ? this.addForm.facultyIds.join(',') : '',	// 学院ID
          "facultyCns": facultyCns, // 学院名称 
          'inputType': this.addForm.enterType.toString(), // 可录入类型
          // 'inputTypeDetails': this.addForm.relatedEnteredTypes, // 关联可录入类型成绩
          // 'inputTypeCode': this.finalScoreCodeList,
          'totalInputType': this.addForm.totalStandardRecord.toString(), // 总评成绩记录方式
          'reexamInputType': this.addForm.reExamRecord.toString() // 重考成绩记录方式
        }
        scoreManagementApi.addOrSaveScoreEntrySwitch(params).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: this.$t('scoreMangementBase.addSuccessfully')
            })
            this.backToMain()
          }
        })
      },
      // 取消
      modelCancel() {
        Object.keys(this.addForm).forEach(key => this.addForm[key] = '')
        this.addForm.facultyIds = []
        this.$refs.addForm.clearValidate()
        this.backToMain()
      }
    }
}
</script>
<style lang="scss" scoped>
  .add-right-container ::v-deep {
    .el-breadcrumb__item:last-child {
      .el-breadcrumb__inner {
        color: #606266;
        font-weight: bold;
        cursor: default
      }
    }
    .el-breadcrumb__item {
      .is-link {
        color: #aaa;
        font-weight: unset;
      }
      .is-link:hover {
        color: #409eff;
      }
    }
    .el-transfer {
      width: 100%;
    }
    .el-transfer .el-transfer-panel {
      width: calc(50% - 81px);
      height: 100%;
    }
  }
</style>
